package com.gzdh.dishrecognition.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.TextureView;

/**
 * Created by wangyt on 2019/4/25
 */
public class AutoFitTextureView extends TextureView {

    private static final String TAG = "AutoFitTextureView";
    private int ratioW = 0;
    private int ratioH = 0;

    private int realWidth;

    private int realHeight;

    private ScaleGestureDetector mScaleDetector;

    public AutoFitTextureView(Context context) {
        super(context);
    }

    public AutoFitTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoFitTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置宽高比
     * @param width
     * @param height
     */
    public void setAspectRation(int width, int height){
        if (width < 0 || height < 0){
            throw new IllegalArgumentException("width or height can not be negative.");
        }
        ratioW = width;
        ratioH = height;
        //请求重新布局
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (0 == ratioW || 0 == ratioH){
            //未设定宽高比，使用预览窗口默认宽高
            setMeasuredDimension(width, height);
        }else {
            //设定宽高比，调整预览窗口大小（调整后窗口大小不超过默认值）
            if (width < height * ratioW / ratioH){
                realWidth = width;
                realHeight = width * ratioH / ratioW;
                setMeasuredDimension(width, width * ratioH / ratioW);
            }else {
                realWidth = height * ratioW / ratioH;
                realHeight = height;
            }
            Log.e(TAG, "realWidth:" + realWidth + "------realHeight:" + realHeight);
            setMeasuredDimension(realWidth, realHeight);
        }

    }

    public int getRealHeight() {
        return realHeight;
    }

    public int getRealWidth() {
        return realWidth;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mScaleDetector != null)
            return mScaleDetector.onTouchEvent(event);
        return true;
    }

    public void setScaleDetector(ScaleGestureDetector mScaleDetector) {
        this.mScaleDetector = mScaleDetector;
    }
}
